package Chapter21.homework;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 杨森
 * @date 2022年11月09日 19:34
 */
public class Homework01Client {
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket(InetAddress.getLocalHost(),9999);

        OutputStream outputStream=socket.getOutputStream();

        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(outputStream));
//        从键盘读取用户的问题
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的问题");
        String question = scanner.next();

        bw.write(question);
        bw.newLine();
        bw.flush();

//4. 获取和socket关联的输入流. 读取数据(字符)，并显示
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        System.out.println(s);

        //5. 关闭流对象和socket, 必须关闭
        bufferedReader.close();//关闭外层流
        bw.close();
        socket.close();
        System.out.println("客户端退出.....");

    }
}
